package com.bookyourtrek.action;

import java.sql.SQLException;

import com.bookyourtrek.model.LoginModel;
import com.bookyourtrek.util.EmailManager;
import com.bookyourtrek.util.RuntimeSettings;
import com.bookyourtrek.util.UtilityFunctions;
import com.opensymphony.xwork2.ActionSupport;

public class ForgotPasswordAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	
	private String forgotemail;

	public String getForgotemail() {
		return forgotemail;
	}

	public void setForgotemail(String forgotemail) {
		this.forgotemail = forgotemail;
	}

	public String requestpassword() throws SQLException {
		
		//check weather email exists or not
		//custom validations
				LoginModel loginmodel = new LoginModel();
				
				//checking whether user exist or not
				if(!loginmodel.checkEmail(forgotemail)){
					addFieldError("forgotemail","Email does not exist.");
					return INPUT;
				}
		
				//reset password	
				//generate a random password
				String resetpass = UtilityFunctions.randomStringGenerator();
				loginmodel.updatePassword(forgotemail, UtilityFunctions.generateMD5(resetpass));
				
				//send new password to user
				EmailManager.sendMail(forgotemail, "Password Reset", "You Passwaord is " +
						"being reset to - " + resetpass , RuntimeSettings.smtpFrom, 
						RuntimeSettings.smtpPassword);
				
				
		return SUCCESS;
	}
	
	public String forgotpassword() {
		return SUCCESS;
		
	}
}
